package com.lxmes.base.service.impl;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;

import com.lxmes.base.domain.JcItembzinfo;
import com.lxmes.base.domain.JcIteminfo;
import com.lxmes.base.mapper.JcItembzinfoMapper;
import com.lxmes.base.mapper.JcIteminfoMapper;
import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DzGeneraRuleUtils;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.base.mapper.JcIteminfoGzMapper;
import com.lxmes.base.domain.JcIteminfoGz;
import com.lxmes.base.service.IJcIteminfoGzService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 物料生成规则Service业务层处理
 * 
 * @author 张志煜
 * @date 2022-11-18
 */
@Service
public class JcIteminfoGzServiceImpl implements IJcIteminfoGzService 
{
    @Autowired
    private JcIteminfoGzMapper jcIteminfoGzMapper;
    @Autowired
    private JcItembzinfoMapper itembzinfoMapper;
    @Autowired
    private JcIteminfoMapper iteminfoMapper;

    /**
     * 查询物料生成规则
     * 
     * @param id 物料生成规则主键
     * @return 物料生成规则
     */
    @Override
    public JcIteminfoGz selectJcIteminfoGzById(String id)
    {
        return jcIteminfoGzMapper.selectJcIteminfoGzById(id);
    }

    /**
     * 查询物料生成规则列表
     * 
     * @param jcIteminfoGz 物料生成规则
     * @return 物料生成规则
     */
    @Override
    public List<JcIteminfoGz> selectJcIteminfoGzList(JcIteminfoGz jcIteminfoGz)
    {
        return jcIteminfoGzMapper.selectJcIteminfoGzList(jcIteminfoGz);
    }

    /**
     * 新增物料生成规则
     * 
     * @param jcIteminfoGz 物料生成规则
     * @return 结果
     */
    @Transactional
    @Override
    public int insertJcIteminfoGz(JcIteminfoGz jcIteminfoGz)
    {
        String SEQ ="";
        DecimalFormat df = new DecimalFormat("000000");
        String maxSeq = jcIteminfoGzMapper.selectJcIteminfoMaxSeq(jcIteminfoGz);
        if(maxSeq==null){
            SEQ = df.format(new BigDecimal("1"));
        }else{
            SEQ=df.format(new BigDecimal(maxSeq).add(new BigDecimal("1")));
        }
        String tenantId = jcIteminfoGz.getTenantId();//公司代码简称
        String classId =jcIteminfoGz.getClassId();//类型代码
        String specCode =jcIteminfoGz.getSpecCode();//规格代码
        String spec =jcIteminfoGz.getSpec();//规格
        String nameGc =jcIteminfoGz.getNameGc();//名称构成
        jcIteminfoGz.setSeq(SEQ);//顺序号
        jcIteminfoGz.setItemCode(tenantId+classId+specCode+SEQ);//产品编号
        jcIteminfoGz.setItemName(nameGc+spec);//产品名称
        jcIteminfoGz.setTmh(tenantId+classId+SEQ);//条码号
        JcIteminfoGz gz = jcIteminfoGzMapper.selectJcIteminfoGzByItemCode(jcIteminfoGz.getItemCode());
        if(gz!=null){
            throw new ServiceException(jcIteminfoGz.getItemCode()+"已存在！");
        }
        jcIteminfoGzMapper.insertJcIteminfoGz(jcIteminfoGz);
        /*JcIteminfo jcIteminfo=new JcIteminfo();
        jcIteminfo.setItemCode(jcIteminfoGz.getItemCode());
        jcIteminfo.setItemName(jcIteminfoGz.getItemName());
        jcIteminfo.setTmh(jcIteminfoGz.getTmh());
        jcIteminfo.setSpec(jcIteminfoGz.getSpec());
        jcIteminfo.setClassId(jcIteminfoGz.getClassId());
        jcIteminfo.setClassName(jcIteminfoGz.getClassName());
        jcIteminfo.setUnit(jcIteminfoGz.getUnit());
        jcIteminfo.setDeptId(SecurityUtils.getDeptId());
        JcIteminfo iteminfo = iteminfoMapper.selectJcIteminfoByItemCode(jcIteminfoGz.getItemCode());
        if(iteminfo!=null){
            throw new ServiceException(jcIteminfoGz.getItemCode()+"已存在！");
        }
        iteminfoMapper.insertJcIteminfo(jcIteminfo);*/
        return 1;
    }

    /**
     * 修改物料生成规则
     * 
     * @param jcIteminfoGz 物料生成规则
     * @return 结果
     */
    @Override
    public int updateJcIteminfoGz(JcIteminfoGz jcIteminfoGz)
    {
        return jcIteminfoGzMapper.updateJcIteminfoGz(jcIteminfoGz);
    }

    /**
     * 批量删除物料生成规则
     * 
     * @param ids 需要删除的物料生成规则主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteJcIteminfoGzByIds(String[] ids)
    {
        for (int i = 0; i <ids.length ; i++) {
            JcIteminfoGz gz = jcIteminfoGzMapper.selectJcIteminfoGzById(ids[i]);
            List<JcIteminfo> verify = iteminfoMapper.selectJcIteminfoByIdentifyCode(gz.getItemIdentifyCode());
            if("1".equals(gz.getScFlag()) || verify.size()>0){
                throw new ServiceException(gz.getItemIdentifyCode()+"物料识别码已推送物料物料信息删除失败！！！");
            }
            //同步删除基础物料表/*
        /*   if(StringUtils.isNotEmpty(gz.getItemIdentifyCode()) && "1".equals(gz.getScFlag())){
                iteminfoMapper.deleteJcIteminfoByItemIdentifyCode(gz.getItemIdentifyCode());
            }else {
                iteminfoMapper.deleteJcIteminfoByItemCode(gz.getItemCode());
            }*/
            JcItembzinfo bz = itembzinfoMapper.selectJcItembzinfoByItemIdentifyCode(gz.getItemIdentifyCode());
            bz.setScFlag("0");
            itembzinfoMapper.updateJcItembzinfo(bz);
        }
        return jcIteminfoGzMapper.deleteJcIteminfoGzByIds(ids);
    }

    /**
     * 删除物料生成规则信息
     * 
     * @param id 物料生成规则主键
     * @return 结果
     */
    @Override
    public int deleteJcIteminfoGzById(String id)
    {
        return jcIteminfoGzMapper.deleteJcIteminfoGzById(id);
    }

    @Override
    @Transactional
    public int addItemInfo(String[] ids) {
        for(String  id : ids) {
            JcIteminfoGz gz = jcIteminfoGzMapper.selectJcIteminfoGzById(id);
            String[] str = gz.getClassId().split(",");
            String[] strName = gz.getClassName().split(",");
            List<JcIteminfo> verify = iteminfoMapper.selectJcIteminfoByIdentifyCode(gz.getItemIdentifyCode());
            if("1".equals(gz.getScFlag()) || verify.size()>0){
                throw new ServiceException(gz.getItemIdentifyCode()+"物料识别码已推送物料物料信息！！！");
            }
            for (int i = 0; i < str.length; i++) {
                JcIteminfo jc=new JcIteminfo();
                jc.setItemName("法兰"+strName[i]);
                jc.setItemId(DzGeneraRuleUtils.generaItemId(str[i],gz.getSpec(),gz.getItemIdentifyCode()));
                jc.setItemCode(DzGeneraRuleUtils.generaItemId(str[i],gz.getSpec(),gz.getItemIdentifyCode()));
                jc.setTmh(DzGeneraRuleUtils.generaItemId(str[i],gz.getSpec(),gz.getItemIdentifyCode()));
                jc.setFactoryCode(SecurityUtils.getFactoryCode());
                jc.setFactoryName(SecurityUtils.getFactoryName());
                jc.setClassId(str[i]);
                jc.setClassName(strName[i]);
                jc.setSpec(gz.getSpec());
                jc.setItemIdentifyCode(gz.getItemIdentifyCode());
                jc.setUnit(gz.getUnit());
                iteminfoMapper.insertJcIteminfo(jc);
            }
            //修改生成标记
            gz.setScFlag("1");
            jcIteminfoGzMapper.updateJcIteminfoGz(gz);
        }
        return 1;
    }
}
